<template>
	<uni-popup ref="popup" type="bottom" @change="change">
		<view class="popup-content d-flex flex-column">
			<view class="d-flex justify-content-end">
				<image src="/static/images/common/clousex-big.png" class="close_btn" @tap="close"></image>
			</view>
			<view class="d-flex flex-fill flex-column justify-content-between">
				<view class="d-flex flex-column">
					<view class="font-size-extra-lg font-weight-bold text-color-base mb-30">欢迎登陆小镜界~</view>
					<view class="font-size-base text-color-assist">登录后消费可获取积分，享受更好的服务体验</view>
				</view>
				<view class="d-flex flex-column">
					<!-- <button type="primary" class="w-100 font-size-lg mb-30" open-type="getUserInfo" @getuserinfo="getUserInfo">微信一键登陆</button> -->
					<button type="primary" class="w-100 font-size-lg mb-30" @click="getUserInfo()">微信一键登陆</button>
					<view class="text-center mb-30 font-size-sm text-color-assist">
						点击登陆小镜界，即表示已阅读并同意<font class="text-color-primary">《小镜界隐私政策》</font>
					</view>
					<view class="text-center font-primary font-size-sm text-color-primary">《小镜界小程序服务指南》</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import {
		mapMutations
	} from 'vuex'

	export default {
		name: 'LoginPopup',
		components: {
			uniPopup
		},
		props: {

		},
		methods: {
			...mapMutations(['SET_USERINFO', 'SET_ISLOGIN']),
			open() {
				this.$refs['popup'].open()
			},
			close() {
				this.$refs['popup'].close()
			},
			change({
				show
			}) {
				this.$emit('change', show)
			},
			getUserInfo() {
				wx.getUserProfile({
					lang: 'zh_CN',
					desc: "获取你的昵称、头像、地区及性别",
					success: res => {
						console.log(res)
						let uinfo = res.userInfo;
						
						this.saveUserInfoToServer(uinfo);
						this.SET_USERINFO(uinfo)
						this.SET_ISLOGIN(true)
						this.close()
					},
					fail: res => {
						//拒绝授权
						that.showErrorModal('您拒绝了请求');
						return;
					}
				});

			},
			//按钮授权 备份
			getUserInfo2(e) {
				//请配置AppID，否则获取失败
				if (e.target.errMsg !== 'getUserInfo:ok') {
					uni.showModal({
						title: '提示',
						content: '您已取消了授权，请重新授权登录',
						showCancel: false
					})
					return
				}
				let uinfo = e.target.userInfo;

				//test
				// let uinfo={
				// 	avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKZD2icB9exJnxvVViaBR0KucTIrzuphMI7qz1TOLoibM3rGgVAEaEntVTxs5vF9KH9OiaK6MPeRjIShg/132",
				// 	city: "Fuzhou",
				// 	country: "China",
				// 	gender: 1,
				// 	language: "zh_CN",
				// 	nickName: "水清",
				// 	province: "Fujian"
				// };
				//  uni.setStorageSync('WxId', 7);
				//end  test

				console.log(uinfo);
				this.saveUserInfoToServer(uinfo);
				this.SET_USERINFO(uinfo)
				this.SET_ISLOGIN(true)
				this.close()
			},

			//保存用户信息
			saveUserInfoToServer(res_user) {
				let that = this;
				wx.login({
					success: function(res) {
						console.log(res.code)
						if (res.code) {
							let params = [];
							params.push("jscode=" + res.code);
							params.push("userid=" + uni.getStorageSync('coid'));
							params.push("nickname=" + res_user.nickName);
							params.push("sex=" + res_user.gender);
							params.push("headimgurl=" + res_user.avatarUrl);
							params.push("country=" + res_user.country);
							params.push("province=" + res_user.province);
							params.push("city=" + res_user.city);

							that.saveUserInfo(params.join('&'));
						}
					}
				});
			},

			//保存数据至服务端
			saveUserInfo(params) {
				let that = this;
				let cateOp = {
					url: `api/Sys_User/ChatAuth?${params}`,
					data: {}
				}

				that.$request.Post(cateOp, (ret) => {
					if (ret.result.data.length > 0) {
						let data = ret.result.data[0];
						uni.setStorageSync('WxId', data.id);
						uni.setStorageSync('wxOpenId', data.wxOpenId);
						uni.setStorageSync('sessionKey', data.creator);
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #FFFFFF;
		border-radius: 24rpx 24rpx 0 0;
		height: 50vh;
		padding: 50rpx 40rpx;
	}

	.close_btn {
		width: 40rpx;
		height: 40rpx;
	}
</style>
